// pages/order/order.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderArray: [], //订单数据
    currentOrderArray: [], //当前已经获取的订单
    currentPage: 0, //当前获取的页数
    pageSize: 10, //一页的订单数
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    setInterval(function () {
      that.getOrder();
      console.log(2);
    }, 1000)
  },
  request: function(kind) {
    var self = this;
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url,
      data: {
        page: self.data.page,
        rows: self.data.rows
      },
      success: function(res) {
        wx.hiddenLoading();
        var data = res.data.data.data;
        var page = self.data.page;
        if (kind == 'sou') {
          if (data.list == null) {
            wx.showToast({
              title: "暂无数据",
              icon: 'none',
              duration: 1000
            });

          }
        } else {
          if (data.list != null) {
            page++;
          } else {
            wx.showToast({
              title: "没有更多的数据了...",
              icon: 'none',
              duration: 1000
            });
          }
          var list = self.data.orderList.concat(data.list);
          self.setData({
            orderList: list,
            page: page
          });
        }
      }
    })
  },
  onReachBottom: function() {
    console.log("到底啦！！");
  },
  getOrder: function() {
    var that = this;
    wx.request({
      url: app.globalData.ip + '/api/order',
      method: 'GET',
      header: {
        "X-Token": app.globalData.token
      },
      success: function(e) {
        var _orderArray = e.data.data;
        for (var i in _orderArray) {
          // 0：待确认，1：准备中，2：完结，3：取消',
          _orderArray[i].createTime = app.timeFormat(_orderArray[i].orderEntity.createTime);
          switch (_orderArray[i].orderEntity.state) {
            case 0:
              _orderArray[i].status = "待接单";
              break;
            case 1:
              _orderArray[i].status = "准备中";
              break;
            case 2:
              _orderArray[i].status = "待评价";
              break;
            case 4:
              _orderArray[i].status = "已结束";
              break;
            case 3:
              _orderArray[i].status = "已取消";
              break;
            default:
              break;
          }
        }
        that.setData({
          orderArray: _orderArray
        })
      }
    })
  },
  toOrderDetail: function(e) {
    var _orderId = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '../orderDetail/orderDetail?orderId=' + _orderId,
    })
  },
})